"""
Copyright (c) 2023 - present SKbioinfo
"""
from flask_restful import Resource, reqparse
from flask import request,jsonify
from flask_login import current_user,login_required

from apps.NGS_API.common.db_table_config import *
from apps.NGS_API.connector.db_common import *
from apps.NGS_API.common.common_response import *

class interfaceSidebar(Resource):
    @login_required
    def get(self):
        cols = '''
            tb1.id,
            tb1.name,
            tb1.detail,
            tb1.permissions,
            tb1.default,
            tb2.username,
            tb2.email,
            tb1.group
        '''
        user_info_query_sql = '''
            SELECT 
                {cols}
            FROM {table_one} as tb1
            INNER JOIN {table_two} as tb2 
            ON tb1.{col_one} = tb2.{col_two}
            WHERE tb2.{col_three} = \"{user}\"
        '''.format(
            cols = cols,
            table_one = role_db["name"],
            table_two = user_db["name"],
            col_one = role_db["match"]["role_id"],
            col_two = user_db["match"]["role_id"],
            col_three = user_db["match"]["username"],
            user = current_user.username
        )
        user_base_infos = query_info(user_info_query_sql)
        user_infos = []
        for user in user_base_infos:
            user_infos.append({
                    "id": user[0],
                    "name": user[1],
                    "detail": user[2],
                    "permissions": user[3],
                    "default": user[4],
                    "username": user[5],
                    "email": user[6],
                    "group": user[7]
            })
        response = jsonify(user_infos)
        response.status_code = 202 
        return response
    
